package com.laicunba.ext.web.controller.console.sys;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.laicunba.ext.web.controller.BaseController;
import com.laicunba.ext.web.controller.SuiCunBaoBaseController;
import com.laicunba.pojo.RegistryDO;
import com.laicunba.service.RegistryService;
import com.laicunba.service.mq.ActiveMQMessageFactory;
import com.laicunba.service.mq.ActiveMQService;
import com.laicunba.util.StringUtil;

@Controller
@RequestMapping(value = "/console/ext/sys")
public class JmsTopicToQueueController extends SuiCunBaoBaseController {
	private static final String KEY = "sys.jms.topic.to.queue";
	@Autowired
	private RegistryService registryService;

	@Autowired
	private ActiveMQService activeMQService;

	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping(value = "saveJmsTopicToQueueSetting.htm")
	public String saveJmsTopicToQueueSetting(String gate, String topic, String queue) {
		JSONObject json = new JSONObject();
		if (StringUtil.isEmpty(gate) || Integer.parseInt(gate) > 15) {
			json.put("code", "FAIL1");
			return json.toJSONString();

		}
		boolean isDelMap = StringUtil.isEmpty(topic) && StringUtil.isEmpty(queue);
		boolean isUpdateMap = StringUtil.isNotEmpty(topic) && StringUtil.isNotEmpty(queue);
		if (!isDelMap && !isUpdateMap) {
			json.put("code", "FAIL2");
			return json.toJSONString();
		}
		if (isUpdateMap && (!topic.startsWith("lcb.core.") || !queue.startsWith("lcb.core.tmp."))) {
			json.put("code", "FAIL3");
			return json.toJSONString();
		}

		JSONObject gateJson = new JSONObject();
		gateJson.put("g", gate);
		gateJson.put("t", topic);
		gateJson.put("q", queue);

		JSONArray settingList = new JSONArray();
		boolean update = false;
		RegistryDO registryDO = registryService.get(RegistryDO.class, KEY);
		if (registryDO != null) {
			update = true;
			try {
				settingList = (JSONArray) new JSONParser().parse(registryDO.getVal());
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}

		for (int i = 0; i < settingList.size(); i++) {
			JSONObject tmp = (JSONObject) settingList.get(i);
			if (gate.equalsIgnoreCase(tmp.get("g").toString())) {
				settingList.remove(tmp);
			}
		}
		if (isUpdateMap) {
			settingList.add(gateJson);
		}

		boolean success = update ? registryService.update(KEY, settingList.toJSONString()) : registryService.create(KEY, settingList.toJSONString());

		JSONObject msg = ActiveMQMessageFactory.createEmptyMessage("lcb.core.action.update.jms.map");
		msg.put("gate", gate);
		msg.put("topic", topic);
		msg.put("queue", queue);
		activeMQService.sendQueues("lcb.core.action.update.jms.map", msg.toJSONString(), StringUtil.genGUID());
		json.put("code", success ? "SUCCESS" : "SAVE_ERROR");
		return json.toJSONString();
	}

	@SuppressWarnings("unchecked")
	@ResponseBody
	@RequestMapping(value = "getJmsTopicToQueueSetting.htm")
	public String getJmsTopicToQueueSetting(String gate, String topic, String queue) {
		RegistryDO registryDO = registryService.get(RegistryDO.class, KEY);
		return registryDO != null ? registryDO.getVal() : "[]";
	}

}
